package com.nekonomics
package error

import zio.http.*
import zio.json.*

sealed trait AppError(msg: String) extends Throwable {
  def getMsg: String = msg
  
  override def getMessage: String = msg
  
//  def data[T]: Option[T] = ???
  
  final def errorCode: Int = this match {
    case _: ValidationError => 1000
    case _: UserError => 1100
    case _: TokenError | MerchantError => 1200
    case _: ParamError => 1400
    case _: ProductError => 1500
    case _: OrderError => 1600
    case _: PaymentError => 1700
  }

  final def httpStatus: Status = this match {
    case _: TokenError => Status.Unauthorized
//    case _: UserError | MerchantError => Status.Conflict
    case _: ValidationError => Status.BadRequest
    case _: ParamError => Status.BadRequest
//    case _: ProductError => Status.NotFound
    case _ => Status.Ok
  }
}



enum ParamError(msg: String) extends AppError(msg) {
  case ParamMissing(paramName: String)
    extends ParamError(s"参数 $paramName 缺失")
  case InvalidParam(msg: String)
    extends ParamError(msg)
}

enum TokenError(msg: String) extends AppError(msg) {
  case InvalidToken(msg: String="")
    extends TokenError(s"token不合法${if msg.isEmpty then "" else ":"+msg}")
  case TokenExpired extends TokenError("token已过期")
  case TokenMissing extends TokenError("token缺失")
}


enum UserError(msg: String) extends AppError(msg) {
  case AlreadyRegistered extends UserError("用户已注册")
  case UserNotFound extends UserError("用户不存在")
  case IncorrectPassword extends UserError("密码错误")
  case AddressNotFound extends UserError("订单地址不存在")
}

enum MerchantError(msg: String) extends AppError(msg) {
  case MerchantNotFound extends MerchantError("商户不存在")
}

enum ProductError(msg: String) extends AppError(msg) {
  case SpuNotFound extends ProductError("spu不存在")
  case SkuNotFound extends ProductError("sku不存在")
  case StockInsufficient extends ProductError("库存不足")
  case SkuNotActive extends ProductError("sku已下架")
}


enum OrderError(msg: String) extends  AppError(msg) {
  case OrderNotFound extends OrderError("订单不存在")
  case OrderExpired extends OrderError("订单已过期")
  case OrderNotPaid extends OrderError("订单未支付")
  case OrderNotDelivered extends OrderError("订单未发货")
  case OrderNotReceived extends OrderError("订单未收货")
  case OrderNotCompleted extends OrderError("订单未完成")
  case OrderNotCanceled extends OrderError("订单未取消")
  case OrderAlreadyPaid extends OrderError("订单已支付")
}

enum ValidationError(msg: String) extends AppError(msg) {
  case InvalidEmail extends ValidationError("邮件格式错误")
  case EmptyField(field: String) extends ValidationError(s"$field 不能为空")
  case SpecialSymbol extends ValidationError("只能使用数字和字母组合")
  case InvalidLength(msg: String) extends  ValidationError(msg)

}

enum PaymentError(msg: String) extends AppError(msg) {
  case PaymentFailed extends PaymentError("支付失败")
  case PaymentCanceled extends PaymentError("支付已取消")
  case PaymentNotFound extends PaymentError("支付记录不存在")
}


enum PermissionError(msg: String) extends AppError(msg) {
  case PaymentDenied extends PermissionError("订单只允许本人账户支付")
  case CancelOrderDenied extends PermissionError("订单只允许本人账户取消")
  case PermissionDenied extends PermissionError("权限不足")
}
